package com.z.front;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.z.admin.dao.BlogMapper;
import com.z.admin.dao.UserMapper;
import com.z.admin.entity.Blog;
import com.z.admin.entity.Tag;
import com.z.admin.entity.User;
import com.z.admin.service.IBlogService;
import com.z.admin.service.ICommentService;
import com.z.common.base.PageTableRequest;
import com.z.common.base.ResponseCode;
import com.z.common.base.Results;
import com.z.common.utils.ImageBase64Converter;
import com.z.common.utils.MarkdownUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("f/blog")
public class FBlogController extends BaseController<IBlogService, Blog> {
    @Resource
    private BlogMapper blogMapper;
    @Resource
    private UserMapper userMapper;
    @Autowired
    private ICommentService commentService;
    @Autowired
    private IBlogService blogService;

    @GetMapping("{id}")
    @ApiOperation("公告展示页面")
    public String show(Model model, @PathVariable("id") Long id) {
        Blog blog = blogMapper.selectById(id);
        String content = blog.getContent();
        blog.setContent(MarkdownUtils.markdownToHtmlExtensions(content));
        blog.setFirstPicture("data:image/png;base64," + ImageBase64Converter.convertFileToBase64(blog.getFirstPicture()));
        model.addAttribute("blog", blog);
        List<Tag> tags = blogMapper.getTags(id);
        model.addAttribute("blogTags", tags);
        model.addAttribute("comments", commentService.listCommentByBlogId(id));
        User user = userMapper.selectById(blog.getUserId());
        model.addAttribute("user", user == null ? new User() : user);
        blog.setViews(blog.getViews()+1);
        blogMapper.updateViewsById(id);
        return "front/blog/blogDetail";
    }

    @GetMapping("listPageByMap")
    @ApiOperation("列表分页查询")
    @ResponseBody
    public Results listPageByMap(PageTableRequest page, HttpServletRequest request) {
        page.countOffset();
        String tagId = request.getParameter("tag_id");
        String typeId = request.getParameter("type_id");
        Map<String, Object> map = new HashMap<>();
        if(StringUtils.isNotBlank(tagId)){
            map.put("tags_id/eq/b", 1);
        }
        if(StringUtils.isNotBlank(typeId)){
            map.put("type_id/eq/b", 1);
        }
        map.put("published/eq/b", 1);
        Results<Blog> results = blogService.findPageByMap(map, page.getOffset(), page.getLimit());
        return results;
    }

    @PostMapping("add")
    @ApiOperation("角色添加接口")
    @ResponseBody
    public Results add(Blog t){
        try{
            blogService.save(t);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }
    @PostMapping("update")
    @ApiOperation("角色更新接口")
    @ResponseBody
    public Results update(Blog t){
        try{

            blogService.getBaseMapper().updateById(t);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }
    @PostMapping("del")
    @ApiOperation("角色删除-单个")
    @ResponseBody
    public Results del(String id){
        try{
            blogService.getBaseMapper().deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }
    @PostMapping("delAll")
    @ApiOperation("角色删除接口-多个")
    @ResponseBody
    public Results delAll(String ids){
        try{
            List<String> idList = Arrays.asList(ids.split(","));
            blogService.getBaseMapper().deleteBatchIds(idList);
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }
}
